package com.ulook.lookplanet.ui.square;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.ulook.lookplanet.base.BaseViewModel2;
import com.ulook.lookplanet.data.model.TTopic;
import com.ulook.lookplanet.data.repository.SquareRepository;
import com.ulook.lookplanet.data.source.SquareDataSource;
import com.ulook.lookplanet.util.ThrowException;
import com.ulook.lookplanet.util.json.JacksonUtil;
import com.ulook.lookplanet.util.net.OkHttpException;
import com.ulook.lookplanet.util.net.ResponseCallback;

import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @data 2021/4/6
 */
public class  SquareViewModel extends BaseViewModel2 {

    private SquareRepository repository;
    private MutableLiveData<List<TTopic>> hotTopic = new MutableLiveData<>();
    private MutableLiveData<List<VArticlePreInfo>> loadData = new MutableLiveData<>();

    public SquareViewModel() {
        repository = SquareRepository.getInstance(new SquareDataSource());
    }

    public LiveData<List<TTopic>> getHotTopic(){
        return hotTopic;
    }
    public LiveData<List<VArticlePreInfo>> getLoadData(){
        return loadData;
    }

    /**
     * 查询热点话题
     */
    public void queryHotTopics(){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<TTopic> hotTopics = JacksonUtil.json2list((String)res, TTopic.class);
                    hotTopic.setValue(hotTopics);
                } catch (Exception e) {
                    setToastText("获取热点话题解析数据失败");
                    ThrowException.print(e);
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText("获取热点话题失败");
                ThrowException.print(failuer, failuer.getEmsg());
            }
        };
        repository.queryHotTopics(callback);
    }

    /**
     * 获取推荐动态
     */
    public void loadData(int currUserId, int itemNum){
        ResponseCallback callback = new ResponseCallback() {
            @Override
            public void onSuccess(Object res) {
                try {
                    List<VArticlePreInfo> articlePreInfos = JacksonUtil.json2list((String)res, VArticlePreInfo.class);
                    loadData.setValue(articlePreInfos);
                } catch (Exception e) {
                    setToastText("获取动态解析数据失败");
                    ThrowException.print(e);
                }
            }
            @Override
            public void onFailure(OkHttpException failuer) {
                setToastText(failuer.getEmsg());
                ThrowException.print(failuer, failuer.getEmsg());
            }
        };
        repository.loadData(currUserId, itemNum, callback);
    }

    /**
     * 添加浏览记录
     */
    /*public void addBrowseRecord(int currUserId, String artId){

    }*/
}